package com.example.mybatisplusdemo.web.controller;


import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import com.example.mybatisplusdemo.model.domain.User;
import com.example.mybatisplusdemo.model.dto.UserCenterDTO;
import com.example.mybatisplusdemo.service.UserCenterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;


import java.util.*;

/**
 * <p>
 * 用户中心控制器
 * </p>
 *
 * @author yangpengfei
 * @since 2025/01/27
 */
@Slf4j
@RestController
@RequestMapping("/api/user-center")
@RequiredArgsConstructor
public class UserCenterController {

    private final UserCenterService userCenterService;

    /**
     * 获取用户中心数据
     * 
     * @param request HTTP请求
     * @return BaseResponse<UserCenterDTO> 用户中心数据
     */
    @GetMapping("/data")
    public JsonResponse<UserCenterDTO> getUserCenterData(HttpServletRequest request) {
        try {
            // 从session中获取当前用户
            User user = SessionUtils.getCurrentUserInfo();
            if (user == null) {
                return JsonResponse.failure("用户未登录");
            }
            
            Long userId = user.getId();
            
            // 调用服务层获取用户中心数据
            UserCenterDTO userCenterData = userCenterService.getUserCenterData(userId);
            
            return JsonResponse.success(userCenterData, "获取用户中心数据成功");
        } catch (Exception e) {
            log.error("获取用户中心数据失败", e);
            return JsonResponse.failure( "获取用户中心数据失败: " + e.getMessage());
        }
    }

    /**
     * 提交用户反馈
     * 
     * @param request HTTP请求
     * @param feedbackData 反馈数据
     * @return BaseResponse<String> 提交结果
     */
    @PostMapping("/feedback")
    public JsonResponse<String> submitFeedback(HttpServletRequest request, @RequestBody Map<String, Object> feedbackData) {
        try {
            // 从session中获取当前用户
            User user = SessionUtils.getCurrentUserInfo();
            if (user == null) {
                return JsonResponse.failure("用户未登录");
            }
            
            Long userId = user.getId();
            
            String type = (String) feedbackData.get("type");
            String content = (String) feedbackData.get("content");
            String contact = (String) feedbackData.get("contact");
            
            // 调用服务层提交反馈
            userCenterService.submitFeedback(userId, type, content, contact);
            
            return JsonResponse.success("反馈提交成功，我们会尽快处理您的意见！");
        } catch (Exception e) {
            log.error("提交反馈失败", e);
            return JsonResponse.failure("提交反馈失败: " + e.getMessage());
        }
    }

    /**
     * 刷新推荐内容
     * 
     * @param request HTTP请求
     * @return BaseResponse<Object> 新的推荐内容
     */
    @PostMapping("/refresh-recommendations")
    public JsonResponse<Object> refreshRecommendations(HttpServletRequest request) {
        try {
            // 从session中获取当前用户
            User user = SessionUtils.getCurrentUserInfo();
            if (user == null) {
                return JsonResponse.failure("用户未登录");
            }
            
            Long userId = user.getId();
            
            // 调用服务层生成新的推荐
            Object newRecommendations = userCenterService.generateRecommendations(userId);
            
            return JsonResponse.success(newRecommendations, "刷新推荐成功");
        } catch (Exception e) {
            log.error("刷新推荐失败", e);
            return JsonResponse.failure("刷新推荐失败: " + e.getMessage());
        }
    }
}